<!DOCTYPE html>
<%@page import="java.util.UUID"%>
<%@page import="java.io.File"%>
<%@page import="cn.school.entity.Student"%>
<%@page import="cn.school.service.StudentService"%>
<%@page import="cn.school.util.StringKit"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<html lang="en">
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>在此处插入标题</title>
</head>
<body>
<%
	
	DiskFileItemFactory factory=new DiskFileItemFactory();
	factory.setSizeThreshold(1024*1024);//缓存大小
	ServletFileUpload upload=new ServletFileUpload(factory);
	upload.setHeaderEncoding("utf-8");
	List<FileItem> list=upload.parseRequest(request);
  	Student stu=new Student();
	for(FileItem f:list){
%>
 <p>
  <% 
  String inputName=f.getFieldName();//表单元素的name属性值
  String imgName=f.getName();//上传文件的文件名，普通表单为null
  if(f.isFormField()) {
    if(inputName.equals("stuno")){
	  	stu.setStuno(Long.parseLong(f.getString("utf-8")));
	}else if(inputName.equals("sname")){
  		stu.setSname(f.getString("utf-8"));
  	}else if(inputName.equals("sex")){
  		stu.setSex(f.getString("utf-8"));
  	}else if(inputName.equals("telephone")){
  		//telephone=f.getString("utf-8");
  		stu.setTelephone(f.getString("utf-8"));
  	}else if(inputName.equals("qq")){
  		stu.setQq(f.getString("utf-8"));
  	}else if(inputName.equals("wechat")){
  		stu.setWechat(f.getString("utf-8"));
  	}else if(inputName.equals("gradeid")){
  		stu.setGradeid(StringKit.toLong(f.getString("utf-8")));
  	}else if(inputName.equals("enterdate")){
  	 stu.setEnterdate(StringKit.toDate("utf-8"));
  	}else if(inputName.equals("fromcity")){
  		stu.setFromcity(f.getString("utf-8"));
  	}else if(inputName.equals("birthday")){
  		stu.setBirthday(StringKit.toDate(f.getString("utf-8")));
  	}else if(inputName.equals("address")){
  		stu.setAddress(f.getString("utf-8"));
  	}else if(inputName.equals("pass")){
  		stu.setPass(f.getString("utf-8"));
  	}
  }else{
    if(f.getSize()==0)
    	continue;
    //删除老图片
	String suffix=imgName.substring(imgName.lastIndexOf("."));
  	String newImgName = UUID.randomUUID() + suffix;
  	File path=new File(application.getRealPath("img"));
	  if(path.exists()==false)
		  path.mkdirs();
  
	stu.setIdcardpic(newImgName); 
	try {
		f.write(new java.io.File(path, newImgName));
	} catch (Exception e) {
		out.print("文件太大，上传失败！");
	}
  }
}
  %>
<%
StudentService s=new StudentService();
int r=s.update(stu);
out.print(r>0?"更新成功":"更新失败");
%>
</body>
</html>